package com.dexilaonian.adpter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.dexilaonian.R;

import java.util.List;
import java.util.Map;

public class StoreAdaprer extends PagerAdapter {
    Context context;
    List<Map<String, Object>> viewLists;

    public StoreAdaprer(List<Map<String, Object>> lists, Context context) {
        this.viewLists = lists;
        this.context = context;
    }

    @Override
    public int getCount() {        //获得size
        return viewLists == null ? 0:viewLists.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup view, int position, Object object) {  //销毁Item
        ImageView imageView = (ImageView) viewLists.get(position).get("view");
      //  ImageView imageView = new ImageView(context);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        view.removeView(imageView);
    }

    @Override
    public Object instantiateItem(ViewGroup view, int position){ //实例化Item
       ImageView imageView = (ImageView) viewLists.get(position).get("view");
       // ImageView imageView = new ImageView(context);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        Glide.with(context)
                .load(viewLists.get(position).get("url").toString())
                .placeholder(R.drawable.beijing)
                .error(R.drawable.ic_launcher)
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .into(imageView);
        view.addView(imageView, 0);
        return viewLists.get(position).get("view");
    }
}
